<?php
namespace MobileHome\Controller;

use Think\Controller;
use Think\Exception;

class BaseController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 封装ajax的返回结果
     *
     * @param int $status 状态码
     * @param string $message 提示信息
     * @param array $data 返回值
     *
     * @return mixed
     */
    public function json($status, $message = '', $data = array())
    {
        header('Content-Type:application/json; charset=utf-8');
        $result = [
            'status'  => $status,
            'message' => $message,
            'data'    => $data,
        ];
        echo json_encode($result);
        exit;
    }

    /**
     * 获取通用接口数据
     *
     * 注意:
     *  整车投保: test,uat,prod: 都是 1.0.1
     *  其他接口: test: 1.0.0.9875, prod: 1.0.0
     *
     * @param string $serviceName
     * @param array $params
     * @param $version
     *
     * @return array
     */
    public function getZaCommonDatas($serviceName, $params = [], $version)
    {
        $return = [
            'code'    => 200,
            'message' => '',
        ];
        require_once __DIR__ . '/ZhongAn/ZaCommon/common/ZhongAnApiClient.php';
        try {
            $env = C('ZONGAN.env');
            $appKey = C('ZONGAN.appKey');
            $privateKey = C('ZONGAN.privateKey');
            $_client = new \ZhongAnApiClient($env, $appKey, $privateKey, $serviceName, 'UTF-8', 'RSA', $version);
            $response = $_client->call($params);
        } catch (\Exception $e) {
            $return = [
                'code'    => $e->getCode(),
                'message' => $e->getMessage(),
            ];
        }
        $return['datas'] = $response;
        return $return;
    }
}